package com.coocaa.ops.admin.v1.cornericon.provider.impl;


import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.audit.AuditUtil;
import com.coocaa.ops.admin.v1.cornericon.biz.impl.CornerIconBizImpl;
import com.coocaa.ops.admin.v1.cornericon.provider.ICornerIconUpdateProvider;
import com.coocaa.ops.common.core.base.enums.ResourceStatusEnums;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.audit.entity.AuditTaskInfoEntity;
import com.coocaa.ops.common.core.biz.cornericon.entity.GlobalCornerIconEntity;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author bijiahao
 * @date : 2018/8/17.
 * @description
 */
@Service
public class CornerIconUpdateProviderImpl implements ICornerIconUpdateProvider {
    @Autowired
    private CornerIconBizImpl cornerIconBiz;
    private static CommonLanguageProperties commonLanguageProperties;


    @Override
    public GlobalCornerIconEntity save(GlobalCornerIconEntity entity, Integer userId) {
        if (null == entity.getId()) {
            entity.setStatus(ResourceStatusEnums.TO_AUDITING.getStatus());
            entity.setCreator(userId);
            entity.setModifier(userId);
            cornerIconBiz.add(entity);
        } else {
            entity.setStatus(ResourceStatusEnums.TO_AUDITING.getStatus());
            entity.setModifier(userId);
            cornerIconBiz.updateById(entity);
        }
        return entity;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @param userId
     * @return
     */
    @Override
    public String deleteBatch(Integer[] ids, Integer userId) {
        if (ArrayUtils.isNotEmpty(ids)) {
            for (Integer iconId : ids) {
                GlobalCornerIconEntity iconEntity = cornerIconBiz.queryById(iconId);
                //判断角标是否正在使用
                if (cornerIconBiz.isCornerIsUsedByCurrentPanel(iconId)) {
                    return "[ID：" + iconId + "][" + commonLanguageProperties.getName() + iconEntity.getCornerIconName() + "]" + commonLanguageProperties.getInUse() + commonLanguageProperties.getNotDelete();
                }
                //判断角标是否已提交流程
                AuditTaskInfoEntity taskInfo = AuditUtil.getTaskByIdAndVersionAndType(iconEntity.getId(), null, ResourceTypeEnum.CORNER_ICON.getValue());
                if (taskInfo != null && taskInfo.getTaskFlag() == 1) {
                    return "[ID：" + iconId + "][" + commonLanguageProperties.getName() + iconEntity.getCornerIconName() + "]" + commonLanguageProperties.getSubmitReview() + commonLanguageProperties.getNotDelete();
                }
            }
            //逻辑删除角标
            cornerIconBiz.logicDelBatchCornerByIds(ids);
        }
        return R.SUCCESS;
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        CornerIconUpdateProviderImpl.commonLanguageProperties = commonLanguageProperties;
    }
}
